package com.application.module_chat2.presenter;

import com.application.module_chat2.adapter.ChatAdapter;
import com.application.module_chat2.view.ChatView;
import com.application.mylibrary.base.mvp.IBasePresenter;
import com.application.mylibrary.im.IMMessageUtils;
import com.application.mylibrary.im.ImLocalResultbak;
import com.application.mylibrary.im.MsgType;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.message.TextMessage;

/**
 * Created by lixuce on 2019/8/8.
 */
public class ChatPresenter extends IBasePresenter<ChatView> {
    private String uid;

    public ChatPresenter(String uid) {
        this.uid = uid;
    }

    public void sendTextMessage(String s) {

        TextMessage message = TextMessage.obtain(s);
        RongIMClient.getInstance().sendMessage(Conversation.ConversationType.PRIVATE
                , uid, message, null, null, new IRongCallback.ISendMessageCallback() {
                    @Override
                    public void onAttached(Message message) {

                    }

                    @Override
                    public void onSuccess(Message message) {
                        if (getView() != null) {
                            getView().receiverMessage(handleMessage(message));
                        }
                    }

                    @Override
                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                    }
                });
    }

    // String targetId, int oldestMessageId, int count,
    // final ImLocalResultbak<List<Message>> imLocalResultbak
    public void getMessageHistory(int messageId) {
        IMMessageUtils.getHistoryMessages(Conversation.ConversationType.PRIVATE, uid, messageId, 40, new ImLocalResultbak<List<Message>>() {
            @Override
            public void onSuccess(List<Message> result) {
                List<ChatAdapter.ChatBean> beans = new ArrayList<>();
                if (result != null && result.size() > 0) {
                    for (int i = result.size() - 1; i >= 0; i--) {
                        ChatAdapter.ChatBean chatBean = handleMessage(result.get(i));
                        if (chatBean.getMsgType() > 0) {
                            beans.add(chatBean);
                        }
                    }
                }
                if (getView() != null) {
                    getView().addMessageList(beans);
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode iErrorCode) {

            }
        });
    }
    public void receiverMessage(Message message){
        ChatAdapter.ChatBean chatBean = handleMessage(message);
        if (chatBean.getMsgType() > 0) {
            if (getView()!=null) {
                getView().receiverMessage(chatBean);
            }
        }
    }
    private ChatAdapter.ChatBean handleMessage(Message message) {
        ChatAdapter.ChatBean chatBean = new ChatAdapter.ChatBean();
        MessageContent content = message.getContent();
        chatBean.setTime(message.getSentTime());
        chatBean.setMessageId(message.getMessageId());
        chatBean.setUserId(message.getSenderUserId());
        if (content instanceof TextMessage) {
            if (isSelfMessage(message)) {
                chatBean.setMsgType(MsgType.TYPE_MSG_SELF);
            } else {
                chatBean.setMsgType(MsgType.TYPE_MSG_OTHER);
            }
            chatBean.setMessage(((TextMessage) content).getContent());
        }
        return chatBean;
    }

    private boolean isSelfMessage(Message message) {
        return !message.getSenderUserId().equals(uid);
    }
}
